package com.sunday.android.fangdaifu.bukong.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.android.fangdaifu.bukong.activity.WebViewActivity;
import com.sunday.android.fangdaifu.bukong.activity.index.BuilderDetailActivity;
import com.sunday.android.fangdaifu.bukong.entity.WarnItem;
import com.sunday.android.fangdaifu.bukong.http.ApiClient;
import com.sunday.android.fangdaifu.bukong.utils.Constants;
import com.sunday.common.utils.SpannalbeStringUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yun_sheng on 2017/7/20.
 */

public class BaoListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<WarnItem> warnList;

    public BaoListAdapter(Context context, List<WarnItem> warnList) {
        this.context = context;
        this.warnList = warnList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.bao_list_item, parent, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ItemHolder itemHolder = (ItemHolder) holder;
        final WarnItem item = warnList.get(position);
        String str1 = String.format("所属项目  %s", item.getProjectName());
        String str2 = String.format("项目地点  %s", item.getAddress());
        String str3 = String.format("设备位置  %1s层%2s", item.getFloor(), item.getPointNo());
        String str4 = String.format("实时数据  %s", item.getValue() == null ? 0 : item.getValue());
        String str5 = String.format("设备方位  %s", item.getPosition());
        itemHolder.tvName.setText(item.getPlantNo());
        itemHolder.tvData.setText(SpannalbeStringUtils.setTextColor(str4, Color.parseColor("#333333"), 4, str4.length()));
        itemHolder.tvBelong.setText(SpannalbeStringUtils.setTextColor(str1, Color.parseColor("#333333"), 4, str1.length()));
        itemHolder.tvAddress.setText(SpannalbeStringUtils.setTextColor(str2, Color.parseColor("#333333"), 4, str2.length()));
        itemHolder.tvWhere.setText(SpannalbeStringUtils.setTextColor(str3, Color.parseColor("#333333"), 4, str3.length()));
        itemHolder.tvDirection.setText(SpannalbeStringUtils.setTextColor(str5, Color.parseColor("#333333"), 4, str5.length()));
        itemHolder.lMain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, WebViewActivity.class);
                intent.putExtra(Constants.TITLE,"数据列表");
                String url=ApiClient.API_URL+"/plant/history？plantId="+item.getPlantId();
                intent.putExtra(Constants.URL,url);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return warnList.size();
    }

    //item view
    class ItemHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.tvName)
        TextView tvName;
        @Bind(R.id.tvData)
        TextView tvData;
        @Bind(R.id.tvBelong)
        TextView tvBelong;
        @Bind(R.id.tvAddress)
        TextView tvAddress;
        @Bind(R.id.tvWhere)
        TextView tvWhere;
        @Bind(R.id.lMain)
        LinearLayout lMain;
        @Bind(R.id.tvDirection)
        TextView tvDirection;

        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
